/*******************************************************************************
 ** Name: shell.cpp                                                           **
 ** Description:              **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 ** The contents of this file are subject to the terms of the                 **
 ** Common Development and Distribution License, Version 1.0 only             **
 ** (the "License").  You may not use this file except in compliance          **
 ** with the License.                                                         **
 **                                                                           **
 ** You can find a copy of the license in the license.txt within              **
 ** this distribution or at http://www.munixos.net/licensing.                 **
 ** Software distributed under the License is distributed on an "AS IS"       **
 ** basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.           **
 ** See the License for the specific language governing permissions           **
 ** and limitations under the License.                                        **
 **                                                                           **
 ** When distributing Covered Code, include this CDDL header in each          **
 ** file and include the License file at $HOME/license.txt.                   **
 ** If applicable, add the following below this header, with the indicated    **
 ** fields enclosed by brackets "[]" replaced with your own identifying       **
 ** information: Portions Copyright [yyyy] [name of copyright owner]          **
 **                                                                           **
 **                                                                           **
 ** Copyright (c) 2009-2010  Barry Gian James.                                **
 ** All rights reserved.                                                      **
 **                                                                           **
 ** Ref: $HeadURL$
 ******************************************************************************/
// Portions (C) 2011 OpenHouseware, LLC.
// All Rights Reserved

// $Id$
// Last Modified $DateTime$ by $Author$
#define _MUNIX_SHELL_CPP_ID "$Id$"

#include <string.h>
#include <stdlib.h>
#include "common.h"
#include "shell.h"
#include "progmem.h"

// command handler prototypes
uint8   cmdHelp(char **);
uint8   cmdBoot(char **);

#define MAX_ARGS    6
#define WHITE_SPACE " \r\t\n"

const char *
Shell::prompt = "(atbios ok) ";

biosCmdStruct   biosCmds[] =
{
    { "help", cmdHelp },
    { "boot", cmdBoot },

	{ NULL, NULL }
};

// TODO
uint8
Shell::ExecCmd(char* cmd, char** argv)
{
	uint8	i = 0;
	while(biosCmds[i].name)
	{
		if (!strcmp(cmd,biosCmds[i].name))
			return biosCmds[i].biosCmdFunc(argv);
		++i;
	}
    return 0;
}

uint8
Shell::Process(char* b)
{
    char    buff[32];
    memcpy(buff,b,32);

    uint8 idx = 0;
    char * cmd, * ptr, * args[MAX_ARGS];
    if (strtok(buff,WHITE_SPACE) == NULL)
        return 1;
    cmd = buff;

    while( (ptr = strtok(NULL,WHITE_SPACE)) != NULL)
    {
        args[idx] = ptr;
        if (++idx == (MAX_ARGS-1))
            break;
    }
    args[idx] = NULL;

    return ExecCmd(cmd,args);
}

Shell::Shell()
{
//    yyparse();
}

Shell::~Shell()
{

}

/////////////////////[ command handlers
uint8 cmdHelp(char * args)
{

    return 0;
}

uint8 cmdBoot(char * args)
{

    return 0;
}
